<template>
    <div>
        <a-modal
            :width="1000"
            title="选择模板"
            :visible="selectTemplateVisible"
            @ok="selectTemplateHandleOk"
            @cancel="selectTemplateVisible = false"
            :forceRender="true"
            :dialog-style="{top:40+'px'}"
        >
            <template slot="footer">
                <div style="display: flex;justify-content: flex-end;align-items: center;">
                    <div style="color: red;font-size: 16px;">（注意：重新选择模板，会初始化文档的内容。）</div>
                    <a-form layout="inline" style="display: flex;justify-content: flex-end;">
                        <a-form-item>
                            <a-button  @click="selectTemplateVisible = false" icon>关闭</a-button>
                        </a-form-item>
                        <a-form-item>
                            <a-button :loading="okLoading" class="blue-btn" type="primary" @click="selectTemplateHandleOk">确认</a-button>
                        </a-form-item>
                    </a-form>
                </div>
            </template>
            <commpVxeTable
                :key="selectTemplateKey"
                :config="selectTemplateConfig" 
                :columns="selectTemplatecolumnsVxe" 
                :formItem="selectTemplateformItem"
                @radioChange="selectTemplateChange"
                ref="acceptanceRegistrationRef"
                :defaultSelected="{key:'xzid'}"
                @getDefaultSelected="getDefaultSelected"
            >
            </commpVxeTable>
            
    </a-modal>
    </div>
</template>
<script>
import commpVxeTable from '@/components/commp-vxe-table/commp-vxe-table.vue'
import assessCheckApi from './service/api'
import {  selectWjmbServe,commoninitWjjl, mbzjsaveSelectSqsxWjmb, sjxdinitSjxdMb } from './service/index'
import blobType from '@/common/blobType'
export default {
    components:{
        commpVxeTable,
    },
    props:{

    },
    data(){
        return{
            selectTemplateVisible:false,
            selectTemplateKey:null,
            templateId:'',
            takeNotesId:'',
            takeNotesType:'',
            selectTemplateConfig:{
                api:assessCheckApi.queryYwflWjmbList,
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                height:400,
                autoHidden:true,
                pageSize:100,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                }
            },
            selectTemplatecolumnsVxe:[{
                type:'radio',
                width:60,
            },{
                title: '模板标题', //列头显示文字
                field: 'fmbbt',
                key: 'fmbbt',
            },{
                title: '文件类型', //列头显示文字
                field: 'fwjlx',
                key: 'fwjlx',
            },{
                title: '模板类型1', //列头显示文字
                field: 'fmblx1',
                key: 'fmblx1',
            }],
            sqsxid:'',
            selectTemplateformItem:[{
                label: '模板标题',
                model: 'fmbbt',
            }],
            selectFileSaveType:false,
            currentRow:{},
            functionData:{
                selectWjmbServe:selectWjmbServe,
                commoninitWjjl:commoninitWjjl,
                mbzjsaveSelectSqsxWjmb:mbzjsaveSelectSqsxWjmb,
                sjxdinitSjxdMb:sjxdinitSjxdMb,
            },
            okLoading:false,
            parametData:{},
        }
    },
    watch:{

    },
    mounted(){
        
    },
    methods:{
        getSelectTemplate(row,val,sqsxid,fhbsxzt){
            this.okLoading = false
            //console.log('选择模板的数据',row)
            this.currentRow = row
            this.selectFileSaveType = row.selectFileSaveType
            this.sqsxid = sqsxid
            this.templateId = ''
            // this.selectTemplateVisible = true
            if(sqsxid && typeof sqsxid !== 'string'){
                this.parametData = sqsxid
            }else{
                this.parametData.sqsxid = sqsxid
            }
            this.selectTemplateConfig = {
                api:fhbsxzt == 1?assessCheckApi.spcyRsWjmbqueryHbjcMbList:(row.listApi?assessCheckApi[row.listApi]:assessCheckApi.queryYwflWjmbList),
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                height:400,
                autoHidden:true,
                pageSize:100,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                    type:val,
                    ...this.parametData,
                }
            },
            this.selectTemplateKey = Date.now()
            this.takeNotesId = row.id
            this.takeNotesType = val
        },
        getDefaultSelected(val){
            this.templateId = val
        },
        selectTemplateChange(val){
            this.templateId = val.row.id
        },
        selectTemplateHandleOk(){
            if(this.templateId){
                this.okLoading = true
                let data = {}
                if(this.currentRow.okFun == 'sjxdinitSjxdMb'){
                    data={
                        id:this.parametData.id,
                        fmbid:this.templateId,
                    }
                }else{
                    data = {fileid:this.takeNotesId,sqsxid:this.sqsxid,type:this.takeNotesType,id:this.templateId}
                }
                this.functionData[this.currentRow.okFun?this.currentRow.okFun:(this.selectFileSaveType?this.selectFileSaveType:'selectWjmbServe')](data).then(res=>{
                    if(res.code == 200){
                        // this.manualSuccess()
                        this.$emit('selectTemplateComplete',this.takeNotesType,this.currentRow)
                        this.$message.success('已选择模板')
                        this.templateId = ''
                        this.takeNotesId = ''
                        this.takeNotesType = ''
                        this.selectTemplateVisible = false
                        
                    }else{
                        this.$message.warning(res.message)
                    }
                    this.okLoading = false
                })
            }else{
                this.$message.warning('请选择模板')
            }
            
        },
    }
}
</script>